package gdut.jmxywz.action;


import gdut.jmxywz.been.MessageBoard;
import gdut.jmxywz.been.MessageBoardFile;
import gdut.jmxywz.been.MessageBoardReply;
import gdut.jmxywz.been.StmessageBoard;
import gdut.jmxywz.been.StmessageBoardFile;
import gdut.jmxywz.been.StmessageReply;
import gdut.jmxywz.been.Student;
import gdut.jmxywz.been.Teacher;
import gdut.jmxywz.util.PageModel;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("serial")
public class StudentMessageBoardAction extends BaseAction {
	
	private  static final String  TEACHER_SESSION_KEY = "teacherSessionKey";
	private  static final String STUDENT_SESSION_KEY="studentSessionKey"; 
	
	private StmessageBoard studentMessageBoard ;
	
	private StmessageReply  stmessageBoardReply;
	
	private StmessageBoardFile  stmessageBoardFile;
	
	private Teacher teacher;
	private Student student;
	//文件
	private String[] files_content;
	private String[] files_title;
	private List<File> upload_file;
	private String[] files_save_as;
	private String[] upload_fileContentType;
	private String[] upload_fileFileName;
	private String   savePath;
	
	private List<StmessageBoardFile>  stmessageBoardFileList;
	 
	private List<StmessageReply>      stmessageBoardReplyList;
	
	//分页
	private int        currentPage=1;
	private int        pageSize=5;
	private String     nextPage;
	private PageModel  pm;
	
	//查询条件
	private  String   where_search=""; 
	private  String   where_value;
	private  String   like ;

	//删除所有回复
	private String   replyAll;
	private String   auditing;
	
	
	
	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public StmessageReply getStmessageBoardReply() {
		return stmessageBoardReply;
	}

	public void setStmessageBoardReply(StmessageReply stmessageBoardReply) {
		this.stmessageBoardReply = stmessageBoardReply;
	}

	public List<StmessageReply> getStmessageBoardReplyList() {
		return stmessageBoardReplyList;
	}

	public void setStmessageBoardReplyList(
			List<StmessageReply> stmessageBoardReplyList) {
		this.stmessageBoardReplyList = stmessageBoardReplyList;
	}

	public StmessageBoard getStudentMessageBoard() {
		return studentMessageBoard;
	}

	public void setStudentMessageBoard(StmessageBoard studentMessageBoard) {
		this.studentMessageBoard = studentMessageBoard;
	}

	public StmessageReply getstmessageBoardReply() {
		return stmessageBoardReply;
	}

	public void setstmessageBoardReply(StmessageReply stmessageBoardReply) {
		this.stmessageBoardReply = stmessageBoardReply;
	}

	public StmessageBoardFile getStmessageBoardFile() {
		return stmessageBoardFile;
	}

	public void setStmessageBoardFile(StmessageBoardFile stmessageBoardFile) {
		this.stmessageBoardFile = stmessageBoardFile;
	}

	public String[] getFiles_content() {
		return files_content;
	}

	public void setFiles_content(String[] filesContent) {
		files_content = filesContent;
	}

	public String[] getFiles_title() {
		return files_title;
	}

	public void setFiles_title(String[] filesTitle) {
		files_title = filesTitle;
	}

	public List<File> getUpload_file() {
		return upload_file;
	}

	public void setUpload_file(List<File> uploadFile) {
		upload_file = uploadFile;
	}

	public String[] getFiles_save_as() {
		return files_save_as;
	}

	public void setFiles_save_as(String[] filesSaveAs) {
		files_save_as = filesSaveAs;
	}

	public String[] getUpload_fileContentType() {
		return upload_fileContentType;
	}

	public void setUpload_fileContentType(String[] uploadFileContentType) {
		upload_fileContentType = uploadFileContentType;
	}

	public String[] getUpload_fileFileName() {
		return upload_fileFileName;
	}

	public void setUpload_fileFileName(String[] uploadFileFileName) {
		upload_fileFileName = uploadFileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public List<StmessageBoardFile> getStmessageBoardFileList() {
		return stmessageBoardFileList;
	}

	public void setStmessageBoardFileList(
			List<StmessageBoardFile> stmessageBoardFileList) {
		this.stmessageBoardFileList = stmessageBoardFileList;
	}

	public List<StmessageReply> getstmessageBoardReplyList() {
		return stmessageBoardReplyList;
	}

	public void setstmessageBoardReplyList(List<StmessageReply> stmessageBoardReplyList) {
		this.stmessageBoardReplyList = stmessageBoardReplyList;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getNextPage() {
		return nextPage;
	}

	public void setNextPage(String nextPage) {
		this.nextPage = nextPage;
	}

	public PageModel getPm() {
		return pm;
	}

	public void setPm(PageModel pm) {
		this.pm = pm;
	}

	public String getWhere_search() {
		return where_search;
	}

	public void setWhere_search(String whereSearch) {
		where_search = whereSearch;
	}

	public String getWhere_value() {
		return where_value;
	}

	public void setWhere_value(String whereValue) {
		where_value = whereValue;
	}

	public String getLike() {
		return like;
	}

	public void setLike(String like) {
		this.like = like;
	}

	public String getReplyAll() {
		return replyAll;
	}

	public void setReplyAll(String replyAll) {
		this.replyAll = replyAll;
	}

	public String getAuditing() {
		return auditing;
	}

	public void setAuditing(String auditing) {
		this.auditing = auditing;
	}

	/***
	 *  发布留言
	 * @return
	 * @throws Exception
	 */
	public String Add() throws Exception{
		
		student =(Student)ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY);
		
	    List<File> files=getUpload_file();
		
		List file_list = new ArrayList();
		
		setSavePath("");//上传路径
		
		boolean  b = false;
		boolean  b2 =false;
		
		System.out.println(studentMessageBoard.getMessageContext());
		
		studentMessageBoard.setStudent(student);
		studentMessageBoard.setAuditing("1");
		
		b=this.getCommonService().saveObject(studentMessageBoard);
		

		//处理文件上传

		if(files!=null)
			for(int i=0;i<files.size();i++){//控制上传部分

				String extend_name=getUpload_fileFileName()[i];
				System.out.println(extend_name);
				int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
				extend_name=extend_name.substring(extend_name_int);
							
				java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
			   String datestr = df.format(new java.util.Date()) ;
			   Random rand = new Random();//生成随机数
			   int ram = rand.nextInt(500);
				String ram_str = String.valueOf(ram);
				for(int i_i=0; ram_str.length()<6;i_i++){
					ram_str="0"+ram_str;
				}
				
				extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
				String Save_path="\\messageBoardFiles\\"+extend_name;//文件在服务器保存的相对路径
				
				FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
				
				FileInputStream fis=new FileInputStream(files.get(i));
				byte[] buffer=new byte[1024];
				int len=0;
				while((len=fis.read(buffer))>0){
				fos.write(buffer,0,len);
				
				}
				
			    StmessageBoardFile mfile = new StmessageBoardFile();
				
				System.out.println();
				System.out.println("----------第--"+(i+1)+"--文件--------------");
				System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
				System.out.println("--------文件名:"+files_title[i]+"--------------");
				System.out.println("--------文件说明:"+files_content[i]+"--------------");
				
				mfile.setFileUrl(Save_path);
				mfile.setFileName(files_title[i]);
				mfile.setFileContext(files_content[i]);
				mfile.setStmessageBoard(studentMessageBoard);
				mfile.setAuditing("0");
				mfile.setFileDownTime(0);
				
				//插入附件表
				b2=this.getCommonService().saveObject(mfile);
		
		//文件上传处理结束
		
     	}
		
		if(b){
			return "StudentMessageBoardAdd";
		}else{
			return "error";
		}
	
	}
	
	/***
	 *  显示所有帖子
	 * @return
	 * @throws Exception
	 */
	public String ListAll()throws Exception{
		
		String hql ="from StmessageBoard where auditing in(1,2)";
		
		nextPage = "studengtMessageBoardListAll.action";
		
 	  if(!"".equals(where_value)&&where_value!=null){
    		  
    		  System.out.println("++++-- "+where_value);
    		  if(like.equals("like"))
    		  {
             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
    		  }else{
    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
    		  }
    		  
    	  }
       hql = hql +" order by messageBoardTime desc,messageBoardid desc";
		
       System.out.println("====="+hql);
		
		pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
		
		
		return "StudentMessageBoardListAll";
	}
	
	
	
	/***
	 * 留言详细
	 * @return
	 * @throws Exception
	 */
	 public String Detail() throws Exception{
		 

		 System.out.println("--id--"+studentMessageBoard.getMessageBoardid());
		 
		 String hql ="from StmessageBoardFile mf  where mf.auditing in(1,2) and mf.stmessageBoard.messageBoardid="+studentMessageBoard.getMessageBoardid();
		 
		 String hql2="from StmessageReply mr where mr.auditing in(1,2) and mr.stmessageBoard.messageBoardid="+studentMessageBoard.getMessageBoardid(); 
		 
		 hql2 = hql2 + "order by messageBoardReplyTime desc , messageBoardReplyId desc";
		 studentMessageBoard=(StmessageBoard)this.getCommonService().findByIntId(studentMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.StmessageBoard");
		 
		 System.out.println(studentMessageBoard.getMessageContext());
		 
		 
		 //判断是否有附件
           		 
		 stmessageBoardFileList = this.getCommonService().findListByHql(hql, null);
		 
//		 //是否有回复
		 
		 String nextPage = "studentMessageBoardDetail.action?studentMessageBoard.messageBoardid="+studentMessageBoard.getMessageBoardid()+"&";
		 
		 pm = this.getCommonService().findPage(hql2, null, currentPage, pageSize, nextPage, "");
		 
		 System.out.println("============"+pm.getPageMessage());
		 
//		 messageBoardReply = this.getCommonService().findListByHql(hql2, null);
		 
		 
    	// 阅读次数加一
		 
		 studentMessageBoard.setReadtimes(studentMessageBoard.getReadtimes()+1);
		 
		 this.getCommonService().updateObject(studentMessageBoard);
		 
		 
		 
		return "StudentMessageBoardDetail";
	 }
	 
	 

		/***
		 *   留言审核详细
		 * @return
		 * @throws Exception
		 */
		 public String AuditingDetail() throws Exception{
			 

			 System.out.println("--id--"+studentMessageBoard.getMessageBoardid());
			 
			 String hql ="from StmessageBoardFile mf where mf.stmessageBoard.messageBoardid="+studentMessageBoard.getMessageBoardid();
			 
			 String hql2="from StmessageReply mr where mr.stmessageBoard.messageBoardid="+studentMessageBoard.getMessageBoardid(); 
			 
			 hql2 = hql2+ "order by auditing asc, messageBoardReplyTime desc , messageBoardReplyId desc";
			 
			 studentMessageBoard=(StmessageBoard)this.getCommonService().findByIntId(studentMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.StmessageBoard");
			 
			 System.out.println(studentMessageBoard.getMessageContext());
			 
			 //判断是否有附件
	           		 
			 stmessageBoardFileList = this.getCommonService().findListByHql(hql, null);
			 
//			 //是否有回复
			 
			 String nextPage = "studentMessageBoardAuditingDetail.action?studentMessageBoard.messageBoardid="+studentMessageBoard.getMessageBoardid()+"&";
			 
			 pm = this.getCommonService().findPage(hql2, null, currentPage, pageSize, nextPage, "");
			 
			 System.out.println("============"+pm.getPageMessage());
			 
			 
			 
			return "StudentMessageBoardAuditingDetail";
		 }
	 
		 public void Reply()throws Exception{
			 
			 if(ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY)!=null){
				   teacher = (Teacher)ActionContext.getContext().getSession().get(TEACHER_SESSION_KEY);
			 }
			 if(ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY)!=null){
				  student =(Student)ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY);
			 }
			    HttpServletResponse res = ServletActionContext.getResponse();
			    
			    res.setCharacterEncoding("utf-8");
			    
			    res.setContentType("text/xml");
			    PrintWriter out = res.getWriter();   
				  
//				 存入角色
			    
			     System.out.println("+++++++++++++++++++++++++++++++++++++");
			     stmessageBoardReply.setAuditing("1");
			     if(student!=null){
			    	 stmessageBoardReply.setStudent(student);
			     }
				 if(teacher!=null){
					 stmessageBoardReply.setTeacher(teacher);
				 }
				 
			    this.getCommonService().saveObject(stmessageBoardReply);
			    
			    try {   
		            out.print("<root>");   
		            out.print("<message>");   
		            out.print("yes");   
		            out.print("</message>");   
		            out.print("</root>");   
			        } catch (Exception e) {   
			            e.printStackTrace();   
			        } finally {   
			            out.flush();   
			            out.close();   
			        }    	 
			 
		 }
		 
//	 /***
//    *  更换新编辑器，保留代码
//	  * 留言回复--
//	  * 异步交互,实现插入回复内容，并同时返回新回复的内容---//同时实现回复的修改
//	  * @return
//	  * @throws Exception
//	  */
//	 public void Reply() throws Exception{
//		    
//	    HttpServletResponse res = ServletActionContext.getResponse();
//	    
//	    res.setCharacterEncoding("utf-8");
//	    
//	    res.setContentType("text/xml");
//	    
//	    PrintWriter out = res.getWriter();   
//		  
//		 
//	    
//		 boolean b=false;
//	
//		 //未审核
//		 System.out.println("------大方大方的-----");
//		 
//		 System.out.println("--context----"+stmessageBoardReply.getMessageBoardReplyTime());
//		 
//		 stmessageBoardReply.setAuditing("0");
//		 
////		 存入角色
////		 stmessageBoardReply.setStudent(student);
////		 stmessageBoardReply.setTeacher(teacher);
////	    
//		
//		 
//	  
//		    
//	    b=this.getCommonService().saveObject(stmessageBoardReply);
//	    
//	    System.out.println("-----------");
//	    
//		String hql = "from StmessageReply mbr where mbr.messageBoardReplyId="+stmessageBoardReply.getMessageBoardReplyId();
//	    
//	    System.out.println("---"+stmessageBoardReply.getMessageBoardReplyId());
//	    
//	    System.out.println("+++hql++"+hql);
//	    
////	    messageReply =(MessageBoardReply)this.getCommonService().findByIntId(messageReply.getMessageBoardReplyId(), "gdut.jmxywz.been.MessageBoardReply");
//		 
//		 //返回页面的回复信息
//		 
//	        /***
//	         * 
//	         * XMLUtils
//	         */
//	        Document document = XMLUtils.newDocument();
//	        
//		    Element  rspNode = XMLUtils.addElement(document, "param",new Attr[]{});
//
//		    XMLUtils.addTextElement(rspNode,"studentMessageBoardId" ,null,new Attr[]{XMLUtils.createAttribute(document,"value",String.valueOf(studentMessageBoard.getMessageBoardid()))});
//	    	
//		    List listAll = this.getCommonService().findListByHql(hql, null);
//		    
//		 System.out.println("++++++++++++++++++++++++++===");
//		       Iterator it = listAll.iterator();
//		       while(it.hasNext()){
//		    	   String replyName="匿名者";
//		    	   
//		    	   StmessageReply mbr= (StmessageReply)it.next();
//			    
//		    	   Element chiElement = XMLUtils.addTextElement(rspNode,"reply", null,null);
//			       
//		    	   if(mbr.getTeacher()==null){
//			  			 
//			  			 replyName="匿名者";
//			  		 }
//			  		 else{
//			  			 replyName=mbr.getTeacher().getTeacherName();
//			  		 }
//			  		 if(mbr.getStudent() ==null){
//			  			 
//			  			 replyName="匿名者";
//			  		 }
//			  		 else{
//			  			 replyName=mbr.getStudent().getStudentName();
//			  		 }
//		    	   
//			       XMLUtils.addTextElement(chiElement, "replyName", replyName);
//			       
//			       XMLUtils.addTextElement(chiElement, "replyId", String.valueOf(mbr.getMessageBoardReplyId()));
//			       
//			       XMLUtils.addTextElement(chiElement, "replyAuditing", mbr.getAuditing());
//			       
//			       XMLUtils.addTextElement(chiElement, "replyTime",String.valueOf(mbr.getMessageBoardReplyTime().toString().substring(0,10)));
//			       
//			       XMLUtils.addTextElement(chiElement, "context", mbr.getContext());
//
//		       }
//		       
//		       System.out.println("-----"+XMLUtils.toXMLString(document));
//		       
//		       out.println(XMLUtils.toXMLString(document));
//
////		 return "TeacehrMessageBoardReply";
//	        
//	 }
	 
	 
	 /***
	  * 按角色删除留言
	  * @return
	  * @throws Exception
	  */
	 
	 public String DeleteBoard() throws Exception{
		 
		 System.out.println("----delete--------");
		 
		 String hql ="from StmessageReply mbr where ";
		
		 
		 //删除留言同时将所有回复删掉
		 if(studentMessageBoard!=null){
			 if(studentMessageBoard.getMessageBoardid()!=null){
				 
				 System.out.println("--删除留言--"+studentMessageBoard.getMessageBoardid());
				 
				 //删除全部回复
				 hql=hql+" mbr.stmessageBoard.messageBoardid = " + studentMessageBoard.getMessageBoardid() ;
				
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 StmessageReply smbr = (StmessageReply)iter.next();
					 
					 this.getCommonService().deleteObject(smbr);
					 
					 System.out.println("所有回复删除成功"); 
				 }
				 //删除留言
				 this.getCommonService().deleteObject(studentMessageBoard);
				 
			 }
		 }
		 
		 
		 return "StudentMessageBoardDeleteBoard";
	 }
	 
	 public String DeleteReply() throws Exception{
		 
		 String hql ="from StmessageReply mbr where ";
		 
		 //删除全部回复,replyAll==1时为删除所有回复。
		 if(studentMessageBoard!=null&&"1".equals(replyAll)){
			 
			 if(studentMessageBoard.getMessageBoardid()!=null){
				hql=hql+" mbr.stmessageBoard.messageBoardid = " + studentMessageBoard.getMessageBoardid() ;
				
				System.out.println("----删除所有留言回复-------");
				 
			 }else {
				 hql =hql+" 1=1 ";
			 }
			 
		 }
		 
		 //删除一条回复
		 if(stmessageBoardReply!=null){	
			 if(stmessageBoardReply.getMessageBoardReplyId()!=null){
				 
				 hql = hql+" mbr.messageBoardReplyId = "+stmessageBoardReply.getMessageBoardReplyId();
				 
				 System.out.println("----删除一条留言回复-------");
			 }
		 }
		 
		 System.out.println("hql--"+hql);
		 
		 List replyList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator iter = replyList.iterator();
		 
		 while(iter.hasNext()){
			 
			 StmessageReply mbr = (StmessageReply)iter.next();
			 
			 this.getCommonService().deleteObject(mbr);
			 
			 System.out.println("回复删除成功");
		 }
		 
		 
		 return "StudentMessageBoardDeleteReply";
	 }
	 
	 
	 /***
	  *   编辑留言
	  * @return
	  * @throws Exception
	  */

	 
	 public String Modify() throws Exception{
		 
		 System.out.println("++++");
		 
		   List<File> files=getUpload_file();
			
			List file_list = new ArrayList();
			
			setSavePath("");//上传路径
			
			boolean  b = false;
			boolean  b2 =false;
			
			System.out.println(studentMessageBoard.getMessageBoardid());
			
			System.out.println(studentMessageBoard.getMessageTitle());
			
			System.out.println(studentMessageBoard.getMessageContext());
			
			int id =studentMessageBoard.getMessageBoardid();
			
			StmessageBoard mb =(StmessageBoard)this.getCommonService().findByIntId(id,"gdut.jmxywz.been.StmessageBoard");
			
			mb.setMessageTitle(studentMessageBoard.getMessageTitle());
			mb.setMessageContext(studentMessageBoard.getMessageContext());
			mb.setReplylocked(studentMessageBoard.getReplylocked());
			
			
			b=this.getCommonService().updateObject(mb);
			

			//处理文件上传

			if(files!=null)
				for(int i=0;i<files.size();i++){//控制上传部分

					String extend_name=getUpload_fileFileName()[i];
					System.out.println(extend_name);
					int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
					extend_name=extend_name.substring(extend_name_int);
								
					java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
				   String datestr = df.format(new java.util.Date()) ;
				   Random rand = new Random();//生成随机数
				   int ram = rand.nextInt(500);
					String ram_str = String.valueOf(ram);
					for(int i_i=0; ram_str.length()<6;i_i++){
						ram_str="0"+ram_str;
					}
					
					extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
					String Save_path="\\messageBoardFiles\\"+extend_name;//文件在服务器保存的相对路径
					
					FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
					
					FileInputStream fis=new FileInputStream(files.get(i));
					byte[] buffer=new byte[1024];
					int len=0;
					while((len=fis.read(buffer))>0){
					fos.write(buffer,0,len);
					
					}
					
				    StmessageBoardFile mfile = new StmessageBoardFile();
					
					System.out.println();
					System.out.println("----------第--"+(i+1)+"--文件--------------");
					System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
					System.out.println("--------文件名:"+files_title[i]+"--------------");
					System.out.println("--------文件说明:"+files_content[i]+"--------------");
					
					mfile.setFileUrl(Save_path);
					mfile.setFileName(files_title[i]);
					mfile.setFileContext(files_content[i]);
					mfile.setStmessageBoard(mb);
					mfile.setFileDownTime(0);
					mfile.setAuditing("0");
					
					//插入附件表
					b2=this.getCommonService().saveObject(mfile);
			
			//文件上传处理结束
			
	        	}
		 
		 return "StudentMessageBoardModify";
	 }
	 
	 
	 /***
	  *  显示修改留言页面
	  * @return
	  * @throws Exception
	  */
	 @SuppressWarnings("unchecked")
	 
	public String ModifyShow() throws Exception{
		 
		 System.out.println("========");
		 
		 int id = studentMessageBoard.getMessageBoardid();
		 
		 System.out.println("---"+id);
		 
		 String hql ="from StmessageBoardFile mbf where mbf.stmessageBoard.messageBoardid="+id;

		 System.out.println("---"+hql);
		 
		 studentMessageBoard=(StmessageBoard) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.StmessageBoard");
		 
		 
		 //附件
		 
		 stmessageBoardFileList = this.getCommonService().findListByHql(hql, null);
		 
		 Iterator iter = stmessageBoardFileList.iterator();
		 
		 while(iter.hasNext()){
			 
			 StmessageBoardFile sbf =(StmessageBoardFile)iter.next();
			 
			 System.out.println(sbf.getFileContext());
		 }
		 
		 return "StudentMessageBoardModifyShow";
	 }
	 
	 
	 /***
	  *  显示留言审核
	  * 
	  * @return
	  * @throws Exception
	  */
	 
	 public String AuditingBoard()throws Exception{
		
		    String hql ="from StmessageBoard ";
			
			nextPage = "StudentMessageBoardAuditingBoard.action";
			
	 	  if(!"".equals(where_value)&&where_value!=null){
	    		  
	    		  System.out.println("++++-- "+where_value);
	    		  if(like.equals("like"))
	    		  {
	             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
	    		  }else{
	    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
	    		  }
	    		  
	    	  }
	 	  
	 	  if(!"".equals(auditing)&&auditing!=null){
	 		  
	 		  hql = hql +" where auditing = '"+auditing+" '";
	 	  }
	 	  
	 	  hql = hql +"order by auditing,messageBoardTime,messageBoardid desc";
			
	       System.out.println("====="+hql);
			
			pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
			
		 
		 return "StudentMessageBoardAuditingBoard";
	 }
	 
  
	 /***
	  * 审核为院内显示
	  * @return
	  * @throws Exception
	  */
	 public String Inside() throws Exception{
			 
		 System.out.println("id===="+studentMessageBoard.getMessageBoardid());
		 
		 StmessageBoard mb = (StmessageBoard)this.getCommonService().findByIntId(studentMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.StmessageBoard");
		 
	 	 mb.setAuditing("1");
		 
		 try {
			this.getCommonService().updateObject(mb);
			
			System.out.println("审核为院内");
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	   return "StudentMessageBoardInside";
	 }
		  
	    /***
	     *   Outside 留言对外公布
	     */
	    public String Outside() throws Exception{
			  
	    	 System.out.println("id===="+studentMessageBoard.getMessageBoardid());
			 
			 StmessageBoard mb = (StmessageBoard)this.getCommonService().findByIntId(studentMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.StmessageBoard");
			 
		 	 mb.setAuditing("2");
			 
			 try {
				this.getCommonService().updateObject(mb);
				
				System.out.println("审核为院外");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	    	  
	       return "StudentMessageBoardOutside";
		 }
		
		/***
		 * 
		 *    Notallow 留言不通过
		 */
	    public String Notallow() throws Exception{
			  
	    	 System.out.println("id===="+studentMessageBoard.getMessageBoardid());
			 
			 StmessageBoard mb = (StmessageBoard)this.getCommonService().findByIntId(studentMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.StmessageBoard");
			 
		 	 mb.setAuditing("3");
			 
			 try {
				this.getCommonService().updateObject(mb);
				
				System.out.println("审核为院内");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	  	  
			  return "StudentMessageBoardNotallow";
		  }
		
		/***
		 * 
		 *    Rollback 留言未审核
		 */
	    public String Rollback() throws Exception{
			  
	    	 System.out.println("id===="+studentMessageBoard.getMessageBoardid());
			 
			 StmessageBoard mb = (StmessageBoard)this.getCommonService().findByIntId(studentMessageBoard.getMessageBoardid(), "gdut.jmxywz.been.StmessageBoard");
			 
		 	 mb.setAuditing("0");
			 
			 try {
				this.getCommonService().updateObject(mb);
				
				System.out.println("审核为院内");
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

	  		  return "StudentMessageBoardRollback";
	  	  }
	    
	 
	
	    /***
		  *  回复审核为院内显示
		  * @return
		  * @throws Exception
		  */
		 public String  ReplyInside() throws Exception{
			 
			 String hql = " from  StmessageReply mbr where ";
			 
			 //审核所有回复
			 if(studentMessageBoard!=null&&"1".equals(replyAll)){
				 
				 hql = hql+" mbr.stmessageBoard.messageBoardid = "+studentMessageBoard.getMessageBoardid();
				 
			 }else{
				 hql =hql +" 1 = 1 ";
			 }
			 if(stmessageBoardReply!=null){
				 
				 hql =hql +" and mbr.messageBoardReplyId = "+ stmessageBoardReply.getMessageBoardReplyId();
			 }
			 System.out.println("--hql---"+hql);
			 
			 List replyList = this.getCommonService().findListByHql(hql, null);
			 
			 Iterator iter = replyList.iterator();
			 
			 while(iter.hasNext()){
				 
				 StmessageReply mbr = (StmessageReply)iter.next();
				 
				 mbr.setAuditing("1");
				 
				 try {
					 
					this.getCommonService().updateObject(mbr);
					
				} catch (Exception e) {
					// TODO: handle exception
				}
			 }

			 
		   return "StudentMessageBoardReplyInside";
		 }
			  
		    /***
		     *   Outside  回复审核为对外公布
		     */
		    public String ReplyOutside() throws Exception{
		    	
		    	 String hql = " from  StmessageReply mbr where ";
				 
				 //审核所有回复
				 if(studentMessageBoard!=null&&"1".equals(replyAll)){
					 
					 hql = hql+" mbr.stmessageBoard.messageBoardid = "+studentMessageBoard.getMessageBoardid();
					 
				 }else{
					 hql =hql +" 1 = 1 ";
				 }
				 if(stmessageBoardReply!=null){
					 
					 hql =hql +" and mbr.messageBoardReplyId = "+ stmessageBoardReply.getMessageBoardReplyId();
				 }
				 System.out.println("--hql---"+hql);
				 
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 StmessageReply mbr = (StmessageReply)iter.next();
					 
					 mbr.setAuditing("2");
					 
					 try {
						 
						this.getCommonService().updateObject(mbr);
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				 }

		       return "StudentMessageBoardReplyOutside";
			 }
			
			/***
			 * 
			 *    Notallow 回复审核为不通过
			 */
		    public String ReplyNotallow() throws Exception{
				  
		    	 String hql = " from  StmessageReply mbr where ";
				 
				 //审核所有回复
				 if(studentMessageBoard!=null&&"1".equals(replyAll)){
					 
					 hql = hql+" mbr.stmessageBoard.messageBoardid = "+studentMessageBoard.getMessageBoardid();
					 
				 }else{
					 hql =hql +" 1 = 1 ";
				 }
				 if(stmessageBoardReply!=null){
					 
					 hql =hql +" and mbr.messageBoardReplyId = "+ stmessageBoardReply.getMessageBoardReplyId();
				 }
				 System.out.println("--hql---"+hql);
				 
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 StmessageReply mbr = (StmessageReply)iter.next();
					 
					 mbr.setAuditing("3");
					 
					 try {
						 
						this.getCommonService().updateObject(mbr);
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				 }


				  return "StudentMessageBoardReplyNotallow";
			  }
			
			/***
			 * 
			 *    Rollback 回复审核为未审核
			 */
		    public String ReplyRollback() throws Exception{
				  
		    	 String hql = " from  StmessageReply mbr where ";
				 
				 //审核所有回复
				 if(studentMessageBoard!=null&&"1".equals(replyAll)){
					 
					 hql = hql+" mbr.stmessageBoard.messageBoardid = "+studentMessageBoard.getMessageBoardid();
					 
				 }else{
					 hql =hql +" 1 = 1 ";
				 }
				 if(stmessageBoardReply!=null){
					 
					 hql =hql +" and mbr.messageBoardReplyId = "+ stmessageBoardReply.getMessageBoardReplyId();
				 }
				 System.out.println("--hql---"+hql);
				 
				 List replyList = this.getCommonService().findListByHql(hql, null);
				 
				 Iterator iter = replyList.iterator();
				 
				 while(iter.hasNext()){
					 
					 StmessageReply mbr = (StmessageReply)iter.next();
					 
					 mbr.setAuditing("0");
					 
					 try {
						 
						this.getCommonService().updateObject(mbr);
						
					} catch (Exception e) {
						// TODO: handle exception
					}
				 }

		  		  return "StudentMessageBoardReplyRollback";
		  	  }
		    

			/***
			 * 显示自己的留言
			 * @return
			 * @throws Exception
			 */
			
			public String ListSelf() throws Exception{
				
				student =(Student)ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY);
				
				String hql ="from StmessageBoard smb where smb.student.studentId="+student.getStudentId();//学生或者Session
				
				nextPage = "studengtMessageBoardListSelf.action";
				
		 	  if(!"".equals(where_value)&&where_value!=null){
		    		  
		    		  System.out.println("++++-- "+where_value);
		    		  if(like.equals("like"))
		    		  {
		             	   hql =hql+"and "+where_search+" like '%"+where_value+"%'";
		    		  }else{
		    			  hql = hql +"and  "+where_search+" = '"+where_value+"'";
		    		  }
		    		  
		    	  }
		       hql = hql +" order by messageBoardTime desc,messageBoardid desc";
				
		       System.out.println("====="+hql);
				
				pm=(PageModel) this.getCommonService().findPage(hql, null, currentPage, pageSize, nextPage, "");
				
				return "StudentMessageBoardListSelf";
			}
		   
		 
			    /***
			     * 显示修改本人发表的留言
			     * @return
			     * @throws Exception
			     */
			    public String ModifySelfShow()throws Exception{
			    	 
					 int id = studentMessageBoard.getMessageBoardid();
					 
					 System.out.println("---"+id);
					 
					 String hql ="from StmessageBoardFile mbf where  mbf.stmessageBoard.messageBoardid="+id;

					 System.out.println("---"+hql);
					 
					 studentMessageBoard=(StmessageBoard) this.getCommonService().findByIntId(id, "gdut.jmxywz.been.StmessageBoard");
					 
					 //附件
					 
					 stmessageBoardFileList = this.getCommonService().findListByHql(hql, null);
					 
					 Iterator iter = stmessageBoardFileList.iterator();
					 
					 while(iter.hasNext()){
						 
						 StmessageBoardFile sbf =(StmessageBoardFile)iter.next();
						 
						 System.out.println(sbf.getFileContext());
					 }
			    	
			    	return "StudentMessageBoardModifySelfShow";
			    }
			    
			    /***
			     * 修改本人发表的留言
			     * @return
			     * @throws Exception
			     */
			    public String ModifySelf()throws Exception{
			    	
			    	   student =(Student)ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY);
			    	   
			    	   System.out.println("++++");
				  	 
					   List<File> files=getUpload_file();
						
						List file_list = new ArrayList();
						
						setSavePath("");//上传路径
						
						boolean  b = false;
						boolean  b2 =false;
						
						System.out.println(studentMessageBoard.getMessageBoardid());
						
						System.out.println(studentMessageBoard.getMessageTitle());
						
						System.out.println(studentMessageBoard.getMessageContext());
						
						int id =studentMessageBoard.getMessageBoardid();
						
						StmessageBoard mb =(StmessageBoard)this.getCommonService().findByIntId(id,"gdut.jmxywz.been.StmessageBoard");
						
						if(mb.getStudent().getStudentId()!=student.getStudentId()){
							
							System.out.println("不能修改不是自己发表的留言");
							return "error";
						}
						
						mb.setMessageTitle(studentMessageBoard.getMessageTitle());
						mb.setMessageContext(studentMessageBoard.getMessageContext());
						mb.setReplylocked(studentMessageBoard.getReplylocked());
						
						b=this.getCommonService().updateObject(mb);

						//处理文件上传

						if(files!=null)
							for(int i=0;i<files.size();i++){//控制上传部分

								String extend_name=getUpload_fileFileName()[i];
								System.out.println(extend_name);
								int extend_name_int=extend_name.lastIndexOf(".");//截取扩展名
								extend_name=extend_name.substring(extend_name_int);
											
								java.text.DateFormat df = new java.text.SimpleDateFormat("yyyyMMddHHmmss") ;//生成时间用于重命名文件
							   String datestr = df.format(new java.util.Date()) ;
							   Random rand = new Random();//生成随机数
							   int ram = rand.nextInt(500);
								String ram_str = String.valueOf(ram);
								for(int i_i=0; ram_str.length()<6;i_i++){
									ram_str="0"+ram_str;
								}
								
								extend_name=datestr+ram_str+extend_name;//合成文件名    时间+随机数+扩展名
								String Save_path="\\messageBoardFiles\\"+extend_name;//文件在服务器保存的相对路径
								
								FileOutputStream fos=new  FileOutputStream(getSavePath()+Save_path);
								
								FileInputStream fis=new FileInputStream(files.get(i));
								byte[] buffer=new byte[1024];
								int len=0;
								while((len=fis.read(buffer))>0){
								fos.write(buffer,0,len);
								
								}
								
							    StmessageBoardFile mfile = new StmessageBoardFile();
								
								System.out.println();
								System.out.println("----------第--"+(i+1)+"--文件--------------");
								System.out.println("----服务器保存路径-:---"+Save_path+"--------------");
								System.out.println("--------文件名:"+files_title[i]+"--------------");
								System.out.println("--------文件说明:"+files_content[i]+"--------------");
								
								mfile.setFileUrl(Save_path);
								mfile.setFileName(files_title[i]);
								mfile.setFileContext(files_content[i]);
								mfile.setStmessageBoard(mb);
								mfile.setFileDownTime(0);
								mfile.setAuditing("0");
								
								//插入附件表
								b2=this.getCommonService().saveObject(mfile);
						
						//文件上传处理结束
						
				        	}
			    	
			    	return "StudentMessageBoardModifySelf";
			    }
			    /***
			     * 删除本人发表的留言
			     * @return
			     * @throws Exception
			     */
			    public String DeleteSelf()throws Exception{
			    	 System.out.println("----delete--------");
			    	 student =(Student)ActionContext.getContext().getSession().get(STUDENT_SESSION_KEY);
			    	 
					 String hql ="from StmessageReply mbr where ";
					 
					 //删除留言同时将所有回复删掉
					 if(studentMessageBoard!=null){
						 if(studentMessageBoard.getMessageBoardid()!=null){
							 
							 System.out.println("--删除留言--"+studentMessageBoard.getMessageBoardid());
							 
							 //删除全部回复
							 hql=hql+" mbr.stmessageBoard.messageBoardid = " + studentMessageBoard.getMessageBoardid() ;
							
							 List replyList = this.getCommonService().findListByHql(hql, null);
							 
							 Iterator iter = replyList.iterator();
							 
							 while(iter.hasNext()){
								 
								 StmessageReply smbr = (StmessageReply)iter.next();
								 
								 this.getCommonService().deleteObject(smbr);
								 
								 System.out.println("所有回复删除成功"); 
							 }
							 //删除留言
							 this.getCommonService().deleteObject(studentMessageBoard);
							 
						 }
					 }
			    	
			    	return "StudentMessageBoardDeleteSelf";
			    }
			    /***
			     * 
			     * 锁定回复，不予他人回复
			     * @return
			     * @throws Exception
			     */
			    public String Replylocked()throws Exception{

					System.out.println(studentMessageBoard.getMessageBoardid());
					
					int id =studentMessageBoard.getMessageBoardid();
					
					StmessageBoard mb =(StmessageBoard)this.getCommonService().findByIntId(id,"gdut.jmxywz.been.StmessageBoard");
					
					mb.setReplylocked("0");
					
				    this.getCommonService().updateObject(mb);
				
			    	return "StudentMessageBoardReplylocked";
			    }
	 
		

	    
	
}
